package com.note.simpleshop.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WebUtil
{
	private static ThreadLocal<HttpServletRequest> servletRequest = new ThreadLocal<HttpServletRequest>();
	private static ThreadLocal<HttpServletResponse> servletResponse = new ThreadLocal<HttpServletResponse>();
	public static HttpServletRequest getRequest()
	{
		return servletRequest.get();
	}

	public static HttpServletResponse getResponse()
	{
		return servletResponse.get();
	}

	public static void setRequest(HttpServletRequest request)
	{
		servletRequest.set(request);
	}

	public static void setResponse(HttpServletResponse response)
	{
		servletResponse.set(response);
	}

	public static Integer getInt(String key)
	{
		return getString(key) == null ? null : Integer.valueOf(getString(key));
	}
	public static String getHeader(String key)
	{
		String value = getRequest().getHeader(key);
		if (!StringUtils.isNotBlank(value))
		{
			return null;
		}
		return value;
	}
	public static String getString(String key)
	{
		String value = getRequest().getParameter(key);
		if (!StringUtils.isNotBlank(value))
		{
			return null;
		}
		return value;
	}
	public static Long getLong(String key)
	{
		return getString(key) == null ? null : Long.valueOf(getString(key));
	}
	/**
	 * 获取ip地址
	 * @return
	 */
	public static String getRemoteIp()
	{
		return getRequest().getRemoteHost();
	}
	/**
	 * 作者： xubingbing
	 * 描述：获取文件流字符串
	 * 创建日期：2017年10月27日
	 * @return
	 * @throws IOException 
	 */
	public static String getReqStr() throws IOException
	{
		String str="";
		BufferedReader reader=getRequest().getReader();
		while(reader.ready())
		{
			str=str+reader.readLine();
		}
		return str;
	}
	
	
}
